package com.glx.test.service.Impl;

import com.glx.test.response.RegisterResponse;
import com.glx.test.service.MailService;
import com.glx.test.utils.CodeUtils;
import com.glx.test.utils.RedisConstants;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RegisterResponse registerResponse;
    @Resource
    private JavaMailSender mailSender;

    @Override
    public RegisterResponse registerEmail(String mail) {
        String key= RedisConstants.USER_CODE_KEY+mail;
        //生成六位验证码
        String code = CodeUtils.createCode();

        //发送邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("3107840779@qq.com");
        mailMessage.setTo(mail);
        mailMessage.setSubject("AI编码平台注册验证码");
        mailMessage.setText("欢迎注册AI编码平台，您的验证码为："+code+"，请于60秒内完成验证。");

        try {
            mailSender.send(mailMessage);
            System.out.println("邮件发送成功");
            //将验证码存入redis 60秒
            redisTemplate.opsForValue().set(key,code,60,java.util.concurrent.TimeUnit.SECONDS);
        } catch (MailException e) {
            System.out.println(e);
            System.out.println("邮件发送失败");
            registerResponse.setResult("fail");
            registerResponse.setMessage("邮件发送失败");
            return registerResponse;
        }

        registerResponse.setResult("success");
        registerResponse.setMessage("邮件发送成功");
        return registerResponse;
    }

    @Override
    public RegisterResponse verifyRegisterCode(String mail, String code) {
        if (code==null){
            registerResponse.setResult("fail");
            registerResponse.setMessage("验证码不能为空");
            return registerResponse;
        }
        String key= RedisConstants.USER_CODE_KEY+mail;
        //判断redis有没有这个key
        if(Boolean.FALSE.equals(redisTemplate.hasKey(key))){
            registerResponse.setResult("fail");
            registerResponse.setMessage("验证码已过期");
            return registerResponse;
        }

        //从redis中取出验证码
        String redisCode = redisTemplate.opsForValue().get(key);
        System.out.println("用户验证码："+code);
        System.out.println("redis验证码："+redisCode);
        if (code.equals(redisCode)){
            registerResponse.setResult("success");
            registerResponse.setMessage("验证码正确");
            return registerResponse;
        }else{
            registerResponse.setResult("fail");
            registerResponse.setMessage("验证码错误");
            return registerResponse;
        }

    }


}
